import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;

import local.util.MyPrint;
import local.util.MyStringFormat;

public class GetIndexData {    

    private String url = null;
    private String urlReturnString = null;
    private String startString = null;
    private String endString = null;
    private static boolean save = false;
   
    private Vector vInput= new Vector(10);

    private static Hashtable dataHashTable = new Hashtable();

    public static void main (String args[]) {

	if (args.length < 1) {
	    MyPrint.printLine("=",40);
	    System.out.println("[1] Print Index Data");
	    System.out.println("[2] Save Index Data");
	    System.out.println("[3] Run Report");
	    MyPrint.printLine("=",40);
	    System.exit(0);
	}

	if (args[0].equals("1") || args[0].equals("2")) {
	    String my_url     = "http://finance.yahoo.com/m0";;
	    GetIndexData gd = new GetIndexData ();
	    Vector tempV = new Vector ();
	    Hashtable tempH = new Hashtable ();

	    gd.setUrl(my_url);
	    gd.connectToHttpUrl();

	    tempV = gd.putInVectorByLine(gd.getUrlReturnString());

	    gd.setStartString("Bulletin Board");
	    gd.setEndString("ADVERTISEMENT");

	    tempV = gd.selectUsefulInfo(tempV);
	    gd.printIndexDataHead();
	    gd.printVector(tempV,5);
	    MyPrint.printLine("=",100);
	    tempH = gd.getIndexDataHashtable(tempV);
	
	    if (args[0].equals("2")) {
		gd.saveIndexData(tempH);
	    }
	}
	else if (args[0].equals("3")) {
	    GenerateReport report = new GenerateReport ();
	    Vector t_head_v    = new Vector();
	    Vector t_record_v  = new Vector();
	    Vector record_v    = new Vector ();
	    Hashtable t_head_h = new Hashtable ();
	    String my_query_string ="SELECT * FROM volume_record ORDER BY Exchange, Date";

	    DataBaseQuery my_query = new DataBaseQuery ();

	    my_query.setQueryString (my_query_string);
	    my_query.lookup (my_query_string);

	    t_head_v    = my_query.getTableHeadVector();
	    t_record_v  = my_query.getTableRecordVector();
	    MyPrint.printLine("=",100);

	    report.setIndexRecordVector (t_record_v);
	    record_v = report.getIndexRecordVector();
	    report.printTotalReport(record_v,"NASDAQ");
	    MyPrint.printLine("=",100);
	    /*
	    report.printReport(record_v,"NYSE");
	    MyPrint.printLine("=",100);
	    report.printReport(record_v,"AMEX");
	    MyPrint.printLine("=",100);
	    report.printReport(record_v,"BULLBD");
	    MyPrint.printLine("=",100);
	    */
	}
    }
    

    public void printIndexDataHead () {
	MyPrint.printLine("=",100);
	System.out.println("\t\t\t    NYSE\t\t AMEX \t\tNasdaq \t\tBulletin Board ");
	MyPrint.printLine("-",100);
    }

    public void saveIndexData(Hashtable tempH) {

	DataBaseInsert db_insert = new DataBaseInsert ();
	String query_head = "INSERT INTO volume_record VALUES (";
	DateAndTime dt = new DateAndTime ();
	String query_body = "'"+dt.getCurrentDate()+"',"+"'"+dt.getCurrentTime()+"',"+"'NASDAQ',"+
	    "'"+tempH.get("NASD_UP")+"',"+
	    "'"+tempH.get("NASD_DOWN")+"',"+
	    "'"+tempH.get("NASD_UNCH")+"',"+
	    "'"+tempH.get("NASD_TOTAL")+"',"+
	    "'"+tempH.get("NASD_NEWHIGH")+"',"+
	    "'"+tempH.get("NASD_NEWLOW")+"',"+
	    "'"+tempH.get("NASD_VUP")+"',"+
	    "'"+tempH.get("NASD_VDOWN")+"',"+
	    "'"+tempH.get("NASD_VUNCH")+"',"+
	    "'"+tempH.get("NASD_VTOTAL")+"');";

	db_insert.setQueryString(query_head+query_body);

	db_insert.insertData();
	query_body = "'"+dt.getCurrentDate()+"',"+"'"+dt.getCurrentTime()+"',"+"'AMEX',"+
	    "'"+tempH.get("AMEX_UP")+"',"+
	    "'"+tempH.get("AMEX_DOWN")+"',"+
	    "'"+tempH.get("AMEX_UNCH")+"',"+
	    "'"+tempH.get("AMEX_TOTAL")+"',"+
	    "'"+tempH.get("AMEX_NEWHIGH")+"',"+
	    "'"+tempH.get("AMEX_NEWLOW")+"',"+
	    "'"+tempH.get("AMEX_VUP")+"',"+
	    "'"+tempH.get("AMEX_VDOWN")+"',"+
	    "'"+tempH.get("AMEX_VUNCH")+"',"+
	    "'"+tempH.get("AMEX_VTOTAL")+"');";

	db_insert.setQueryString(query_head+query_body);
	db_insert.insertData();

	query_body = "'"+dt.getCurrentDate()+"',"+"'"+dt.getCurrentTime()+"',"+"'NYSE',"+
	    "'"+tempH.get("NYSE_UP")+"',"+
	    "'"+tempH.get("NYSE_DOWN")+"',"+
	    "'"+tempH.get("NYSE_UNCH")+"',"+
	    "'"+tempH.get("NYSE_TOTAL")+"',"+
	    "'"+tempH.get("NYSE_NEWHIGH")+"',"+
	    "'"+tempH.get("NYSE_NEWLOW")+"',"+
	    "'"+tempH.get("NYSE_VUP")+"',"+
	    "'"+tempH.get("NYSE_VDOWN")+"',"+
	    "'"+tempH.get("NYSE_VUNCH")+"',"+
	    "'"+tempH.get("NYSE_VTOTAL")+"');";

	db_insert.setQueryString(query_head+query_body);
	db_insert.insertData();

	query_body = "'"+dt.getCurrentDate()+"',"+"'"+dt.getCurrentTime()+"',"+"'BULLBD',"+
	    "'"+tempH.get("BULL_UP")+"',"+
	    "'"+tempH.get("BULL_DOWN")+"',"+
	    "'"+tempH.get("BULL_UNCH")+"',"+
	    "'"+tempH.get("BULL_TOTAL")+"',"+
	    "'"+tempH.get("BULL_NEWHIGH")+"',"+
	    "'"+tempH.get("BULL_NEWLOW")+"',"+
	    "'"+tempH.get("BULL_VUP")+"',"+
	    "'"+tempH.get("BULL_VDOWN")+"',"+
	    "'"+tempH.get("BULL_VUNCH")+"',"+
	    "'"+tempH.get("BULL_VTOTAL")+"');";

	db_insert.setQueryString(query_head+query_body);

	db_insert.insertData();
    }

    public void setUrl (String url_in) {
	url = url_in;
    }

    public String getUrl () {
	return url;
    }

    public void setUrlReturnString (String url_return_string) {
	urlReturnString = url_return_string;
    }

    public String getUrlReturnString () {
	return urlReturnString;
    }

    public void setStartString (String start_string) {
	startString = start_string;
    }

    public void setEndString (String end_string) {
	endString = end_string;
    }
    
    public Vector getIndexDataVector(Vector input_v) {

	Vector out_v = new Vector ();
	String temp = null;

	Enumeration enum = input_v.elements();
	while ( enum.hasMoreElements()) {
	    temp = (String) enum.nextElement();

	    if (temp.equals("Advancing Issues")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }

	    if (temp.equals("Declining Issues")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }
	    if (temp.equals("Unchanged Issues")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }

	    if (temp.equals("Total Issues")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }

	    if (temp.equals("New Highs")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }
	    if (temp.equals("New Lows")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }

	    if (temp.equals("Up Volume")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }

	    if (temp.equals("Down Volume")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }
	    if (temp.equals("Unchanged Volume")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }

	    if (temp.equals("Total Volume")) {
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
		out_v.addElement(enum.nextElement());
	    }
	}
	return (out_v); 
    }

   public Hashtable getIndexDataHashtable (Vector input_v) {

	Hashtable out_htab = new Hashtable ();
	String temp = null;

	Enumeration enum = input_v.elements();
	while ( enum.hasMoreElements()) {
	    temp = (String) enum.nextElement();

	    if (temp.equals("Advancing Issues")) {
		out_htab.put("NYSE_UP", enum.nextElement());
		out_htab.put("AMEX_UP", enum.nextElement());
		out_htab.put("NASD_UP", enum.nextElement());
		out_htab.put("BULL_UP", enum.nextElement());
	    }

	    if (temp.equals("Declining Issues")) {
		out_htab.put("NYSE_DOWN", enum.nextElement());
		out_htab.put("AMEX_DOWN", enum.nextElement());
		out_htab.put("NASD_DOWN", enum.nextElement());
		out_htab.put("BULL_DOWN", enum.nextElement());
	    }
	    if (temp.equals("Unchanged Issues")) {
		out_htab.put("NYSE_UNCH", enum.nextElement());
		out_htab.put("AMEX_UNCH", enum.nextElement());
		out_htab.put("NASD_UNCH", enum.nextElement());
		out_htab.put("BULL_UNCH", enum.nextElement());
	    }

	    if (temp.equals("Total Issues")) {
		out_htab.put("NYSE_TOTAL", enum.nextElement());
		out_htab.put("AMEX_TOTAL", enum.nextElement());
		out_htab.put("NASD_TOTAL", enum.nextElement());
		out_htab.put("BULL_TOTAL", enum.nextElement());
	    }

	    if (temp.equals("New Highs")) {
		out_htab.put("NYSE_NEWHIGH", enum.nextElement());
		out_htab.put("AMEX_NEWHIGH", enum.nextElement());
		out_htab.put("NASD_NEWHIGH", enum.nextElement());
		out_htab.put("BULL_NEWHIGH", enum.nextElement());
	    }
	    if (temp.equals("New Lows")) {
		out_htab.put("NYSE_NEWLOW", enum.nextElement());
		out_htab.put("AMEX_NEWLOW", enum.nextElement());
		out_htab.put("NASD_NEWLOW", enum.nextElement());
		out_htab.put("BULL_NEWLOW", enum.nextElement());
	    }

	    if (temp.equals("Up Volume")) {
		out_htab.put("NYSE_VUP", enum.nextElement());
		out_htab.put("AMEX_VUP", enum.nextElement());
		out_htab.put("NASD_VUP", enum.nextElement());
		out_htab.put("BULL_VUP", enum.nextElement());
	    }

	    if (temp.equals("Down Volume")) {
		out_htab.put("NYSE_VDOWN", enum.nextElement());
		out_htab.put("AMEX_VDOWN", enum.nextElement());
		out_htab.put("NASD_VDOWN", enum.nextElement());
		out_htab.put("BULL_VDOWN", enum.nextElement());
	    }
	    if (temp.equals("Unchanged Volume")) {
		out_htab.put("NYSE_VUNCH", enum.nextElement());
		out_htab.put("AMEX_VUNCH", enum.nextElement());
		out_htab.put("NASD_VUNCH", enum.nextElement());
		out_htab.put("BULL_VUNCH", enum.nextElement());
	    }

	    if (temp.equals("Total Volume")) {
		out_htab.put("NYSE_VTOTAL", enum.nextElement());
		out_htab.put("AMEX_VTOTAL", enum.nextElement());
		out_htab.put("NASD_VTOTAL", enum.nextElement());
		out_htab.put("BULL_VTOTAL", enum.nextElement());
	    }
	}
	return (out_htab); 
    }

    public void connectToHttpsUrl () {

	String urlAddress = getUrl();
	String url_return_string;

	HttpsURLConnection httpsConnection = null;
	String up;

	try {            
	    URL url = new URL (urlAddress);  
	    try {                
		httpsConnection = (HttpsURLConnection)url.openConnection();                
		httpsConnection.setDoOutput(false);                
		httpsConnection.connect();                
		
		System.out.println("Connection Code and Message:" + 
				   httpsConnection.getResponseCode() + 
				   " - " + httpsConnection.getResponseMessage()+"\n");
		
		BufferedReader in = new BufferedReader(new InputStreamReader
		    (httpsConnection.getInputStream()));

		StringBuffer buff= new StringBuffer("");

		url_return_string = in.readLine();

		while (url_return_string != null) {
		    buff.append(url_return_string);
		    url_return_string = in.readLine();
		}

		in.close();            

		url_return_string = buff.toString();
		url_return_string = url_return_string.replaceAll("\n","");
		url_return_string = url_return_string.replaceAll("\t","");
		url_return_string = url_return_string.replaceAll("&nbsp;","");
		url_return_string = url_return_string.replaceAll(">",">\n");
		url_return_string = url_return_string.replaceAll("</","\n</");

		setUrlReturnString(url_return_string);

	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	     System.out.println(mue.getMessage());            
	    System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }

    public void connectToHttpUrl () {

	String urlAddress = getUrl();
	String url_return_string;

	HttpURLConnection httpConnection = null;
	String up;

	try {            
	    URL url = new URL (urlAddress);  
	    try {                
		httpConnection = (HttpURLConnection)url.openConnection();                
		httpConnection.setDoOutput(false);                
		httpConnection.connect();                
		
		System.out.println("Connection Code and Message:" + 
				   httpConnection.getResponseCode() + 
				   " - " + httpConnection.getResponseMessage()+"\n");
		
		BufferedReader in = new BufferedReader(new InputStreamReader
		    (httpConnection.getInputStream()));

		StringBuffer buff= new StringBuffer("");

		url_return_string = in.readLine();

		while (url_return_string != null) {
		    buff.append(url_return_string);
		    url_return_string = in.readLine();
		}

		in.close();            

		url_return_string = buff.toString();
		
		url_return_string = url_return_string.replaceAll("\n","");
		url_return_string = url_return_string.replaceAll("\t","");
		url_return_string = url_return_string.replaceAll("&nbsp;","");
		
		url_return_string = url_return_string.replaceAll(">",">\n");
		url_return_string = url_return_string.replaceAll("</","\n</");
		
		setUrlReturnString(url_return_string);

	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	     System.out.println(mue.getMessage());            
	    System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }

    
    public Vector putInVectorByLine (String input) {

	Vector v = new Vector (10);
	String element = "";
	StringTokenizer tokens = new StringTokenizer(input,"\n");
	while (tokens.hasMoreTokens()) {
	    element = (String)tokens.nextToken();
	    if (element != null) {
		v.addElement(element.trim());
	    }
	}
	
	return v;
    }
    
    public Vector selectUsefulInfo (Vector v_in) {
	Vector v_out = new Vector (10);
	String text = "";
	Enumeration penum = v_in.elements();
       
	while ( penum.hasMoreElements()) {

	    text = (String)penum.nextElement();
	    if (text.startsWith(startString)) {
		break;
	    }
	}
	
	while ( penum.hasMoreElements() ) {
	    text = (String)penum.nextElement();
	    if (text.startsWith(endString)) {
		break;
	    }
	    if (!(text.startsWith("<") ||
		  text.startsWith("+") || 
		  text.startsWith("to<"))) {
		if(!text.equals(""))
		v_out.addElement(text);
	    }
	}
	
	return v_out;
    }

    public static void printVector( Vector inputV, int colum_number) {

	int m = colum_number;
	int n =0;
	String temp = null;
	MyStringFormat sf = new MyStringFormat();
	Enumeration penum = inputV.elements();
	while ( penum.hasMoreElements()) { 
	    temp = (String)(penum.nextElement());
	    if (temp.equals("Total Volume")) {
		MyPrint.printLine("-",100);
	    }

	    System.out.print(sf.rightIndent(temp,18,' '));
	    n++;
	    if (n%m==0) {
		System.out.println();
	    }
	}
    }
}







